package study.sharding.shardingjdbc.support.sharding;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingValue;
import org.assertj.core.util.Lists;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 复合键分片算法
 */
@Slf4j
public class CustomComplexKeysAlgorithm implements ComplexKeysShardingAlgorithm<Long> {

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames,
                                         ComplexKeysShardingValue<Long> shardingValue) {
        //目标名称
        log.info("available_target_names= [{}]", StrUtil.join(",", availableTargetNames));
        //分片信息：分片表
        String logicTableName = shardingValue.getLogicTableName();
//        Map<String, Range<T>> rangeValuesMap = shardingValue.getColumnNameAndRangeValuesMap();
        Map<String, Collection<Long>> valuesMap = shardingValue.getColumnNameAndShardingValuesMap();
        Collection<Long> t1 = valuesMap.get("rb_order_no");
        Collection<Long> t2 = valuesMap.get("rb_begin_time");

        List<String> targetLt = Lists.newArrayList("order_base_2021_1");
        return targetLt;
    }
}
